'''
Description: 
Author: 爱酱丶
Date: 2022-07-22 07:57:49
LastEditTime: 2022-07-27 08:51:47
LastEditors: 爱酱丶
'''
from multiprocessing.pool import ThreadPool
from xml.dom.minidom import parse
import paramiko
import datetime
import zipfile
import glob
import copy
import os
import re

hostname="192.168.1.82"
port="22"
username="root"
password="000000"

hostname="192.168.2.172"
port="22"
username="root"
password="DELL123dell"


# 获取1c含云量
def read_cloud(zipFilePath):
	""""""
	xmlFilePathRe = "^S2.*\.SAFE/MTD_MSIL1C\.xml$"
	zipFile = zipfile.ZipFile(zipFilePath, "r")
	zipFileInfos = zipFile.infolist()
	for zipFileInfo in zipFileInfos:
		if re.match(xmlFilePathRe, zipFileInfo.filename):
			xmlFilePath = zipFile.open(zipFileInfo.filename, "r")
			domTree = parse(xmlFilePath)
			collection = domTree.documentElement
			cloudPercentElement = collection.getElementsByTagName("Cloud_Coverage_Assessment")[0]
			cloudPercent = round(float(copy.deepcopy(cloudPercentElement.childNodes[0].data)))
	return cloudPercent


def remote(hostname,port,username,password,folder,tiles,start_date,end_date,clouds):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
    ssh.connect(hostname=hostname,port=port,username=username,password=password)
    start_date=datetime.datetime.strptime(start_date,"%Y-%m-%d")
    end_date=datetime.datetime.strptime(end_date,"%Y-%m-%d")
    dates = [];temp_date = start_date
    while temp_date <= end_date:
        dates.append(temp_date.strftime("%Y%m%d"))
        temp_date=temp_date+datetime.timedelta(days=1)
    tilesstr = '\('+'\|'.join(tiles)+'\)'
    datesstr = '\('+'\|'.join(dates)+'\)'
    regex = f"'.*S2.*_{datesstr}T.*_T{tilesstr}_.*\.zip$'"
    command = f"find {folder} -regex {regex}"
    _,stdout,_ = ssh.exec_command(command,get_pty=True)
  
    tile_dates = []
    for line in iter(stdout.readline,""):
        path = line.split('\n')[0].replace('\r','')
        if clouds == 100: 
            name = os.path.basename(path)
            date = name.split("_")[2][:8]
            tile = name.split("_")[5][1:]
            tile_dates.append(f"{tile}{date}")
            print(f"{tile}{date}")
            continue
        command = f'sudo zipgrep -n \<Cloud_Coverage_Assessment\> {path}'
        _,stdout_1,_ = ssh.exec_command(command,get_pty=True)
        Assessment = [line_a for line_a in iter(stdout_1.readline,"")][0]
        Assessment = Assessment.split('\n')[0].replace('\r','').split(' ')[-1]
        Assessment = Assessment.replace('<Cloud_Coverage_Assessment>','')
        Assessment = Assessment.replace('</Cloud_Coverage_Assessment>','')
        if clouds >= float(Assessment):
            name = os.path.basename(path)
            date = name.split("_")[2][:8]
            tile = name.split("_")[5][1:]
            tile_dates.append(f"{tile}{date}")
            print(f"{tile}{date}")
    data = []
    for tile in tiles:
        record = []
        for date in dates:
            if f"{tile}{date}" in tile_dates:record.append("OK")
            else: record.append("")
        data.append(record)
    ssh.close()
    return data


def find_file(args):
    folder,tile,date,clouds = args
    restr = f"^S2.*_{date}T.*_T{tile}_.*\.zip$"
    reuslts = []
    for root,_,files in os.walk(folder):
        for file in files:
            if re.match(restr, file):
                path = os.path.join(root, file)
                real_clouds = float(read_cloud(path))
                if real_clouds<=clouds:
                    reuslts.append(f"{real_clouds}") 
    return ','.join(reuslts)


def local(folder,tiles,start_date,end_date,clouds):
    start_date=datetime.datetime.strptime(start_date,"%Y-%m-%d")
    end_date=datetime.datetime.strptime(end_date,"%Y-%m-%d")
    dates = [];temp_date = start_date
    while temp_date <= end_date:
        dates.append(temp_date.strftime("%Y%m%d"))
        temp_date=temp_date+datetime.timedelta(days=1)
    data = []
    for tile in tiles:
        args = [[folder,tile,date,clouds] for date in dates]         
        pool = ThreadPool(16)
        record = pool.map(find_file,args)
        pool.close()
        pool.join()
        data.append(record)
    return data


def main(hostname,port,username,password,folder,tiles,start_date,end_date,clouds):
    if hostname == 'local host':
        data=local(folder,tiles,start_date,end_date,clouds)
    else:
        data=remote(hostname,port,username,password,folder,tiles,start_date,end_date,clouds)
    return data


# if __name__ == "__main__":
#     tiles = ["49SER", "49SES","49SET"]
#     start_date = "2022-07-01"
#     end_date = "2022-07-22"
#     folder = r"/home/homes/Sentinel2Zip"
#     clouds=90
#     main(hostname,port,username,password,folder,tiles,start_date,end_date,clouds)
